package com.oren.base.ctrl;

import org.activiti.engine.ProcessEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: oren
 * @Description:
 * @Date: Create on 2017/11/7 0007 11:01
 */
@Controller
@RequestMapping("/base")
public class BaseController {

	@Autowired
	public ProcessEngine processEngine ;

	@RequestMapping("/toPage")
	public ModelAndView redirectHomePage(HttpServletResponse re, String path){
		ModelAndView mv = new ModelAndView();
		if(path.toLowerCase().startsWith("http://") || path.toLowerCase().startsWith("https://")){
			try {
				re.sendRedirect(path);
			} catch (IOException e) {
				e.printStackTrace();

			}
			return null;
		}else{
			mv.setViewName(path);
		}
		return mv;
	}

	/**
	 * 返回错误信息
	 * @param msg
	 * @return
	 */
	public Map  falseMessage(String msg){
		Map map=new HashMap();
		map.put("status","false");
		map.put("msg",msg);
		return map;
	}

	/**
	 * 返回正确信息
	 * @param msg
	 * @return
	 */
	public Map successMessage(String msg) {
		Map map = new HashMap();
		map.put("status", "success");
		map.put("msg", msg);
		return map;
	}

	//获取session里面的用户名
	public String getSessionUserName(HttpServletRequest request){
		return request.getSession().getAttribute("username").toString();
	}

	//获取session里面的用户id
	public String getSessionUserId(HttpServletRequest request){
		return request.getSession().getAttribute("userId").toString();
	}

}
